<?php

/*
	Class: Commerce_Plugins_Minicart

	About: Author
		Rich Joslin

	About: License
		<http://communit.as/docs/license>
*/
class Commerce_Plugins_Minicart {

	/* Group: Constructors */

	/*
		Function: __construct
	*/
	function __construct() {
	}

	/* Group: Instance Methods */

	/*
		Function: addData
	*/
	function addData($params) {
		memory_profiler("start minicart");
		
		$Cart = new Magento_Curl_Cart();
		$Catalog = new CommerceMagentoCatalog();
		$Session = new Zend_Session_Namespace('Default');
		
		$locale = $Catalog->formatStoreId( $params['locale_code'] );
		$use_locale = Cts_Registry::get('magento_curl_locale_code');
		
		$Cart->setLocale( $locale );
		$Cart->debug_mode = false;
		
		if( $Cart->getCartData() ){
			if( $Cart->num_items > 0 ){
				$params['cart_is_empty']	= false;
				$params['cart_items']		= $Cart->items;
				$params['cart_num_items']	= $Cart->num_items;
				$params['cart_total']		= $Cart->total;
				$params['cart_discount']		= $Cart->discount;
				$params['cart_subtotal']	= $Cart->subtotal;
				$params['cart_urls']		= $Cart->urls;
				
				foreach( $params['cart_items'] as $key=>$item ){
					$params['cart_items'][$key]['configuration'] = array();
					
					## Get the configurable attributes for this product
					list($groups, $info, $details) = Commerce_Common::getProductAttributeDetails( $item['product_id'], $params['locale_code'] );
					//d($info);
					
					foreach( $info as $code=>$attribute ){
						if( !is_null($attribute['value'])
						&& array_key_exists('type',$attribute) && $attribute['type']=='select'
						&& array_key_exists('scope',$attribute) && $attribute['scope']=='global'
						&& array_key_exists('is_configurable',$attribute) && $attribute['is_configurable']=='1'
						&& array_key_exists('apply_to',$attribute) && is_array($attribute['apply_to']) && in_array('simple',$attribute['apply_to']) ){
							foreach( $attribute['options'] as $option ){
								if( $option['value'] == $attribute['value'] ){
									$params['cart_items'][$key]['configuration'][] = array(
										'label'	=> $attribute['label'],
										'value'	=> $option['label']
									);
									break;
								}
							}
						}
					}
					
					## Get the (first) parent so that we get the right media
					$parents = $Catalog->getParentIds( $item['product_id'] );
					$media_product_id = (is_array($parents) && !empty($parents)) ? $parents[0] : $item['product_id'];
					
					$product = $Catalog->getProduct( $media_product_id );
					
					if( array_key_exists('bd_media', $product) && !empty($product['bd_media']) ){
						$media = @unserialize($product['bd_media']);
						if( is_array($media) ){
							if(empty($media['thumb'])) $media['thumb'] = false;
							$params['cart_items'][$key]['media'] = $media[0];
						}
					}
					
					if( array_key_exists('name', $product) && !empty($product['name']) ){
						$params['cart_items'][$key]['name'] = $product['name'];
					}
				}
				
				$cart_url  = Cts_Registry::get('magento_user_scheme').'://';
				$cart_url .= Cts_Registry::get('magento_user_host').'/';
				if( Cts_Registry::get('magento_user_locale_code')=='1' ){
					$cart_url .= $locale.'/';
				}
				
				$cart_url .= 'checkout/cart/';
				
				if( !empty($Session->magento_curl_session_id) ){
					if( Cts_Registry::get('magento_curl_locale_code')=='1' && array_key_exists($locale, $Session->magento_curl_session_id)  ){
						$cart_url .= '?SID='.$Session->magento_curl_session_id[ $locale ];
					} elseif( is_string($Session->magento_curl_session_id) ){
						$cart_url .= '?SID='.$Session->magento_curl_session_id;
					}
				}
				
				$params['cart_view_url'] = $cart_url;
				
				## Add the last item in the cart as the minicart item
				end($params['cart_items']);
				$params['cart_latest_item']	= current($params['cart_items']);
				reset($params['cart_items']);
			} else {
				$params['cart_is_empty']	= true;
				$params['cart_num_items']	= 0;
			}
		} else {
			$params['cart_is_empty']	= true;
			$params['cart_num_items']	= 0;
		}
		
		memory_profiler("end minicart");
		
		return $params;
	}

}
